const ZH_CN = "zh-CN";
const EN_US = "en-US";
const CACHE_KEY = "mooli-lang";

let currentLang = ZH_CN;

export function getLang() {
  return currentLang;
}

export function setLang(lang) {
  currentLang = lang;
  localStorage.setItem(CACHE_KEY, lang);
}

export function setDefaultLang(langFromConfig) {
  const cached = localStorage.getItem(CACHE_KEY);
  if (cached) {
    currentLang = cached;
    return;
  }
  if (navigator.language && navigator.language.indexOf("zh-") !== -1) {
    currentLang = ZH_CN;
  } else {
    currentLang = langFromConfig || EN_US;
  }
}

export function getCurLang(locales) {
  return Object.keys(locales).map((item) => {
    return {
      value: item,
      text: locales[item].langLabel,
    };
  });
};
